<?php

namespace app\api\controller\course;

use app\api\controller\Controller;
use app\api\model\UserTask;
use app\api\service\Message;
use app\common\model\Fitness as FitnessModel;
use app\common\model\Reservation;
use think\Db;


/**
 * 用户取消预约
 * Class Order
 * @package app\api\controller\shop
 */
class User extends Controller
{
   

    
    //取消预约操作(团卡)
    public function cancelCourseAppoint(){
        $request = Request();
        $wxapp_id = $request->param('wxapp_id');

        $reservation_id = $request->Param('reservation_id');//课程id
        if(empty($reservation_id)){
            return $this->renderError('参数不能为空');
        }
        //查看该用户，是否已经授权
        $userData = $this->getUser();

        $courseData = Db::name('course_reservation')->where(['id'=>$reservation_id])->find();
        if(!$courseData){
            return $this->renderError('预约信息不存在');
        }

        if($courseData['user_id'] != $userData['user_id']){
            return $this->renderError('用户信息不匹配');
        }
        
        //当前时间 和 课程开始时间-3小时比较
        $begCancelTime = strtotime( $courseData['start_time'].' -3 hour');

        if(time() > $begCancelTime){
             return $this->renderError('课程开始3小时内，无法取消');
        }
        if($courseData['status'] == 1){
            return $this->renderError('已开始上课,无法修改');
        }
        if($courseData['status'] == 2){
            return $this->renderError('课程已结束,无法修改');
        }
        if($courseData['status'] == 3){
            return $this->renderError('已取消预约,请勿重复');
        }

        //修改预约状态
        $up['status'] = 3;
        Db::name('course_reservation')->where(['id'=>$reservation_id])->update($up);
        //将次数+1 用户
        //选出类型
        Db::name('course_order_detail')->where(['course_order_id'=>$courseData['course_order_id']])->setInc('remain_nums');

        //2023 05 26  17：26取消预约通知教练
        $course = Db::name('course')->where(['id'=>$courseData['course_id']])->find();//课程
        $coach = \app\store\model\store\shop\Coach::detail(['coach_id'=>$courseData['coach_id']]);
        $coach_gz_openid = Db::name('user')->where(['user_id'=>$coach['user_id']])->value('gz_open_id');
        $param['nickName'] = $userData['nickName'];
        $param['phone'] = $userData['phone'];
        $param['course_name'] = $course['course_name'];
        $param['start_time'] = $courseData['start_time'];
        $param['cancel_time'] = time();
        $param['remark'] = '会员取消预约';
        $param['gz_open_id'] = $coach_gz_openid;
        $service = new Message();
        $service->noticeCancel($param);
        return $this->renderSuccess([],'取消预约成功');
    }

    public function test(){
        $service = new Message();
        $course = Db::name('course')->where(['id'=>5])->find();//课程
//        $coach = \app\store\model\store\shop\Coach::detail(['coach_id'=>$courseData['coach_id']]);
//        $coach_gz_openid = Db::name('user')->where(['user_id'=>$coach['user_id']])->value('gz_open_id');
        $param['nickName'] = 'kelly';
        $param['phone'] = '13511110000';
        $param['course_name'] = '测试课程';
        $param['start_time'] = '2023-03-10 20:00';
        $param['cancel_time'] = date('Y-m-d H:i:s');
        $param['remark'] = '会员取消预约';
        $param['gz_open_id'] = 'o1vlg6a8NO7WiA2C6sYqB3pGqCLA';
        $service->noticeCancel($param);
    }

    /**
     * @title 完成作业
     * @author Qian
     * Time: 2023/3/14  17:08
     */
    public function FinishWork(){
        $user = $this->getUser();
        $data = $this->request->param();
        $reservation = Reservation::detail(['id'=>$data['reservation_id']]);//预约记录
        if ($reservation['status'] != 2){
            return $this->renderError('该课程还未下课');
        }
        if (!$this->request->isPost()){
            return $this->renderSuccess($reservation);
        }
        //请求
        $taskModel = new UserTask();
        if ($taskModel->FinishWork($data)){
            return $this->renderSuccess([],'提交成功');
        }
        return $this->renderError($taskModel->getError() ?: '操作失败');
    }

    /**
     * @title 健康数据
     * @author Qian
     * Time: 2023/3/28  17:52
     */
    public function Fitness(){
        $user = $this->getUser();
        $param = $this->request->param();
        //根据type查出数据
        $model = new FitnessModel();
        $ObjList = $model->getList(array_merge(['user_id'=>$user['user_id']],$param));
        $list = $ObjList->toArray()['data'];
        foreach ($list as $k=>$v){
            $list[$k]['data'] = json_decode($v['data'],true);
        }
        return $this->renderSuccess(compact('list'));
    }

    /**
     * @title 数据
     * @param
     * @author Qian
     * Time: 2023/3/29  10:36
     */
    private $fat_config = [
        1=>'体重过瘦型',
        2=>'体重过轻型',
        3=>'正常',
        4=>'体脂肪肌肉型',
        5=>'过多脂肪型',
        6=>'肌肉过重型',
        7=>'轻微肥胖型',
        8=>'肥胖型',
        9=>'过度肥胖型',
    ];
    public function FitnessDetail($id){
        $detail = FitnessModel::detail(['id'=>$id]);
        $fat_config = $this->fat_config;
        return $this->renderSuccess(compact('detail','fat_config'));
    }

    /**
     * @title 获取搜索条件
     * @param
     * @author Qian
     * Time: 2023/3/28  18:06
     */
    public function getCondition($user_id=''){
        if ($user_id == ''){
            $user = $this->getUser();
            $user_id = $user['user_id'];
        }
        $param = $this->request->param();
        //根据type查出教练
        $model = new FitnessModel();
        $list = $model->getCoachAndStage(array_merge(['user_id'=>$user_id],$param));
        return $this->renderSuccess(compact('list'));
    }

}